// pages/news/index.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      current:0,
      category:[],
      category_id:0,
      news:[],
      newsMore: {page:1},
  },
  swiperchange: function (e) {
    this.setData({
      current: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let page = this;
    this.setData({
      isIPhoneX: app.globalData.isIPhoneX
    });
    core.get({url:'/xilujob.common/news_category',loading:false,success(ret,response) {
      if(ret.data.length>0){
        page.setData({category: ret.data,category_id:ret.data[0].id});
        page.fetch();
      }
    }});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      });
    }
  },

  fetch() {
    var category_id = this.data.category_id;
    util.fetch(this, '/xilujob.news/lists', {category_id:category_id}, 'newsMore', 'news', 'data');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  changeCategory: function(e){
    this.setData({
      category_id: e.currentTarget.dataset.id
    })
    this.refresh();
  },

  refresh(){
    this.setData({news:[], newsMore:{page:1}});
    this.fetch();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refresh();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})